﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using Lapas.Common.Forms;
using System.Drawing;

namespace Lapas.Common
{
    public partial class Helper
    {
        public static OExcelDocument GetDocument(string DocumentName)
        {
            System.IO.StreamReader sr = new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(DocumentName));
            XmlDocument doc = new XmlDocument();
            doc.Load(sr);
            sr.Close();
            return new OExcelDocument(doc);
        }

        public static void Export2Excel(DataGridView Grid)
        {
            if (Grid == null) return;
            //if Grid.DatSource==null then???
            //still show error when DataSource is null. review???

            //VanVo modify on 20070725 to export seleted rows on grid
            if (Grid.SelectedRows.Count == 0)
            {
                DisplayMessage.ShowNotice("Hãy chọn dữ liệu cần Export");
                return;
            }
            using (SaveFileDialog saveFileDialog = new SaveFileDialog())
            {
                try
                {
                    saveFileDialog.Filter = "Excel files (*.xls)|*.xls|All files (*.*)|*.*";
                    saveFileDialog.FileName = string.Empty;
                    saveFileDialog.Title = "Xuất ra " + string.Empty;

                    if (saveFileDialog.ShowDialog() == DialogResult.OK && saveFileDialog.FileName.Length > 0)
                    {
                        DataSet dataSet = new DataSet();
                        ObjectAdapter objectAdapter = new ObjectAdapter();
                        DataTable dataTable = new DataTable();

                        //Lay toan bo du lieu trong grid xuat ra Excel
                        foreach (DataGridViewColumn column in Grid.Columns)
                        {

                            dataTable.Columns.Add(column.HeaderText, typeof(string));

                        }

                        foreach (DataGridViewRow row in Grid.SelectedRows)
                        {
                            DataRow dataRow = dataTable.NewRow();

                            foreach (DataGridViewColumn column in Grid.Columns)
                            {
                                // 20070927 TriDM: bo sung neu cot bi null thi tra ra string.Empty
                                if (row.Cells[column.Index].Value == null)
                                {
                                    dataRow[column.Index] = string.Empty;
                                }
                                else
                                {
                                    dataRow[column.Index] = row.Cells[column.Index].Value.ToString();
                                }
                            }
                            dataTable.Rows.Add(dataRow);
                        }

                        //objectAdapter.Fill(dataSet, da);
                        dataSet.Tables.Add(dataTable);
                        ExelUtilities.ExportToExcel(dataSet, saveFileDialog.FileName, string.Empty, string.Empty, string.Empty);
                        DisplayMessage.ShowNotice("Export dữ liệu thành công");
                    }
                }
                catch (Exception ex)
                {
                    DisplayMessage.ShowError(ex.Message);
                }
            }
        }

        //public static OExcelDocument LichSuLenhGiaoDich_Document
        //{
        //    get
        //    {
        //        //System.IO.StreamReader sr = new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("OExcel.Components.Template.LichSuLenhGiaoDich.LichSuLenhGiaoDich.xml"));
        //        //XmlDocument doc = new XmlDocument();
        //        //doc.Load(sr);                
        //        //sr.Close();                
        //        //return new OExcelDocument(doc);
        //        return GetDocument("OExcel.Components.Template.LichSuLenhGiaoDich.LichSuLenhGiaoDich.xml");
        //    }
        //}

        public static OExcelDocument DSSoHuuCKLuuKy_HaNoi
        {
            get { return GetDocument("Lapas.Common.Template.DSSoHuuCK.DSSoHuuCKLuuKy_HaNoi.xml"); }
        }

        public static OExcelDocument DSSoHuuCKLuuKy_HCM
        {
            get { return GetDocument("Lapas.Common.Template.DSSoHuuCK.DSSoHuuCKLuuKy_HCM.xml"); }
        }
        public static OExcelDocument ReportTMTC
        {
            get { return GetDocument("Lapas.Common.Template.BCTC.TMTC.xml"); }
        }

        public static OExcelDocument PhiGiaoDichTheoKhachHang
        {
            get { return GetDocument("Lapas.Common.Template.TradingFee.BaoCaoTheoKhachHang.xml"); }
        }

        public static OExcelDocument TongHopPhiGiaoDichLoai1
        {
            get { return GetDocument("Lapas.Common.Template.TradingFee.BaoCaoTongHopLoai1.xml"); }
        }

        public static OExcelDocument TongHopPhiGiaoDichLoai2
        {
            get { return GetDocument("Lapas.Common.Template.TradingFee.BaoCaoTongHopLoai2.xml"); }
        }

        public static OExcelDocument DSKhachHangMoTaiKhoan
        {
            get { return GetDocument("Lapas.Common.Template.DSSoHuuCK.DSKhachHangMoTaiKhoan.xml"); }
        }

        public static OExcelDocument DSKhachHangDongTaiKhoan
        {
            get { return GetDocument("Lapas.Common.Template.DSSoHuuCK.DSKhachHangDongTaiKhoan.xml"); }
        }

        public static OExcelDocument DSKhachHangUyQuyen
        {
            get { return GetDocument("Lapas.Common.Template.DSSoHuuCK.DSKhachHangUyQuyen.xml"); }
        }

        public static OExcelDocument BaoCaoTinhHinhDongMoTKLK
        {
            get { return GetDocument("Lapas.Common.Template.DSSoHuuCK.BaoCaoTinhHinhDongMoTaiKhoanLuuKy.xml"); }
        }

        public static OExcelDocument DSNhaDauTuDangKyMuaCK
        {
            get { return GetDocument("Lapas.Common.Template.DepositoryExport.Mau17QDK.xml"); }
        }

        public static OExcelDocument DanhSachDangKyThongTinTaiKhoanNDT
        {
            get { return GetDocument("Lapas.Common.Template.DepositoryExport.DanhSachDangKyThongTinTaiKhoanNhaDauTu.xml"); }
        }

        public static OExcelDocument BaoCaoSoLuongTaiKhoanGuiUBCKNN
        {
            get { return GetDocument("Lapas.Common.Template.DSSoHuuCK.BaoCaoSoLuongTaiKhoanGuiUBCKNN.xml"); }
        }

        public static OExcelDocument GiayDangKyMuaChungKhoan
        {
            get { return GetDocument("Lapas.Common.Template.DepositoryExport.Mau20THQ.xml"); }
        }

        public static OExcelDocument DSTongHopChuyenNhuongQuyenMuaCK
        {
            get { return GetDocument("Lapas.Common.Template.RightExec.Mau18THQ.xml"); }
        }

        public static OExcelDocument DanhSachBanChungKhoanLoLe
        {
            get { return GetDocument("Lapas.Common.Template.DepositoryExport.BangKeLoLe.xml"); }
        }

        public static OExcelDocument HoatDongLuuKyChungKhoan
        {
            get { return GetDocument("Lapas.Common.Template.DepositoryExport.BCHoatDongLuuKy.xml"); }
        }

        public static OExcelDocument HoatDongLuuKyChungKhoanNew
        {
            get { return GetDocument("Lapas.Common.Template.DepositoryExport.BCHoatDongLuuKyNew.xml"); }
        }

        public static OExcelDocument DSTongHopNguoiSoHuuCKLuuKy
        {
            get { return GetDocument("Lapas.Common.Template.DSSoHuuCK.DSTongHopNguoiSoHuuCKLuuKy.xml"); }
        }

        public static OExcelDocument DSTongHopNguoiSoHuuCKLuuKy_Version_2
        {
            get { return GetDocument("Lapas.Common.Template.DSSoHuuCK.DSTongHopNguoiSoHuuCKLuuKy_Version_2.xml"); }
        }

        public static OExcelDocument DSTongHopNguoiSoHuuCKLuuKy_HanChe
        {
            get { return GetDocument("Lapas.Common.Template.DSSoHuuCK.DSTongHopNguoiSoHuuCKLuuKy_HanChe.xml"); }
        }

        public static OExcelDocument ForeignInvestSituationTemplate
        {
            get { return GetDocument("Lapas.Common.Template.Customers.ForeignInvestSituation.xml"); }
        }

        public static OExcelDocument ThanhToanCoTucBangTienMat
        {
            get { return GetDocument("Lapas.Common.Template.Customers.ThanhToanCoTucBangTienMat.xml"); }
        }

        public static OExcelDocument ThanhToanCoTucBangCoPhieu
        {
            get { return GetDocument("Lapas.Common.Template.Customers.ThanhToanCoTucBangCoPhieu.xml"); }
        }

        public static OExcelDocument TinhHinhHoatDongMoiGioi
        {
            get { return GetDocument("Lapas.Common.Template.Customers.TongHopTinhHinhMoiGioi.xml"); }
        }

        /// <summary>
        /// Phiếu đối chiếu tài khoản khách hàng
        /// </summary>
        public static OExcelDocument PhieuDoiChieuTaiKhoanKH
        {
            get { return GetDocument("Lapas.Common.Template.Customers.PhieuDoiChieuTaiKhoanKH.xml"); }
        }

        #region "for test only"
        //public static XmlDocument GetXMLDocument(string DocumentName)
        //{
        //    System.IO.StreamReader sr = new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(DocumentName));
        //    //System.IO.StreamReader sr = new System.IO.StreamReader(DocumentName);
        //    XmlDocument doc = new XmlDocument();
        //    doc.Load(sr);
        //    sr.Close();
        //    //return new XmlDocument(doc); //Table
        //    return new XmlDocument();
        //}

        //public static XmlDocument ThongBaoBoSungTSCC
        //{
        //    get { return GetXMLDocument("Lapas.Common.XMLSamples.Mortage.ThongbaobosungTSCC.xml"); }
        //}
        #endregion


        #region DepositoryExport
        public static OExcelDocument DepositoryImportErrorList
        {
            get { return GetDocument("Lapas.Common.Template.DepositoryExport.DepositoryImportErrorList.xml"); }
        }
        #endregion

        #region DSChotDKMuaThem
        public static OExcelDocument DSChotDKMuaThem
        {
            get { return GetDocument("Lapas.Common.Template.RightExec.DSChotDKMuaThem.xml"); }
        }
        #endregion

        #region TongHopThongTinThucHienQuyen
        public static OExcelDocument TongHopThongTinThucHienQuyen
        {
            get { return GetDocument("Lapas.Common.Template.RightExec.TongHopThongTinThucHienQuyen.xml"); }
        }
        #endregion

    }
}
